package com.qf.air.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.air.client.AirSearchClient;
import com.qf.air.config.RabbitMQConfig;
import com.qf.air.entity.Air;
import com.qf.air.enums.AirEnum;
import com.qf.air.exception.AirEx;
import com.qf.air.mapper.AirMapper;
import com.qf.air.service.AirService;
import com.qf.air.service.DistrictService;
import com.qf.air.util.R;
import com.qf.air.vo.ResultVO;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: 维多利加
 * @Date: 2021/3/20 11:02
 */
@Service
public class AirServiceImpl implements AirService {

    @Autowired
    private AirMapper airMapper;

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private DistrictService districtService;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AirSearchClient airSearchClient;

    @Override
    public String findByPageAndDistrictId(Integer page, Integer rows, Integer districtId, String monitoringStation, String startTime, String endTime) throws IOException {

        //2. 封装请求参数
        Map parameter = new HashMap();
        parameter.put("page", page);
        parameter.put("rows", rows);
        parameter.put("districtId", districtId);
        parameter.put("monitoringStation", monitoringStation);
        parameter.put("startTime", startTime);
        parameter.put("endTime", endTime);

        String result  = airSearchClient.list(parameter);
        //5. 返回
        return result;
    }

    @Override
    @Transactional
    public void save(Air air) throws IOException {
        air.setLastModifyTime(new Date());
        Integer count = airMapper.save(air);
        if (count != 1) {
            //添加失败，记录日志，抛出异常
            System.out.println("添加信息失败！ air=" + air);
            throw new AirEx(AirEnum.AIR_ADD_ERROR);
        }
        //封装数据
        // 主键id (主键回填)
        // 检测区域名称 (DistrictService)
        air.setDistrictName(districtService.findDistrictNameById(air.getDistrictId()));

        // 将同步数据到ES的操作，通过RabbitMQ发送消息完成
        String jsonParameter = objectMapper.writeValueAsString(air);
        Map<String,Object> map = new ObjectMapper().readValue(jsonParameter, HashMap.class);


        airSearchClient.add(map);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = -1, readOnly = false, rollbackFor = Exception.class, noRollbackFor = ArithmeticException.class)
    public void deleteById(Integer id) throws JsonProcessingException {
        int i = airMapper.deleteById(id);

        if (i != 1) {
            System.out.println("删除失败！ id=" + id);
            throw new AirEx(AirEnum.AIR_DELETE_ERROR);
        }

        airSearchClient.delete(id+"");

    }

    @Override
    @Transactional
    public void updateById(Air air) {
        int count = airMapper.updateById(air);

        if (count != 1) {
            System.out.println("修改失败！ air=" + air);
            throw new AirEx(AirEnum.AIR_UPDATE_ERROR);
        }
    }

    @Override
    public String findDistrictAirQualityCount() throws IOException {
        // 调用search模块查询统计报表需要的数据
        String result = restTemplate.postForObject("http://localhost:9200/search/echarts", null, String.class);
        return result;
    }
}
